{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 算子融合测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tvm\n",
    "from tvm import relay\n",
    "from tvm.relay import transform\n",
    "from tvm.relay.testing import run_opt_pass\n",
    "import tvm.testing\n",
    "import tvm.topi.testing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def before():\n",
    "    x = relay.var(\"x\", shape=(10, 20))\n",
    "    y = relay.add(x, relay.const(1, \"float32\"))\n",
    "    z = relay.exp(y)\n",
    "    w = relay.squeeze(z)\n",
    "    return relay.Function([x], w)\n",
    "\n",
    "def expected():\n",
    "    x = relay.var(\"p\", shape=(10, 20))\n",
    "    y = relay.add(x, relay.const(1, \"float32\"))\n",
    "    z = relay.exp(y)\n",
    "    w = relay.squeeze(z)\n",
    "    f1 = relay.Function([x], w)\n",
    "    f1 = f1.with_attr(\"Primitive\", tvm.tir.IntImm(\"int32\", 1))\n",
    "    x = relay.var(\"x\", shape=(10, 20))\n",
    "    y = relay.Call(f1, [x])\n",
    "    return relay.Function([x], y)\n",
    "\n",
    "z = before()\n",
    "zz = run_opt_pass(z, transform.FuseOps())\n",
    "after = run_opt_pass(expected(), transform.InferType())\n",
    "assert tvm.ir.structural_equal(zz, after)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "fn (%x: Tensor[(10, 20), float32] /* ty=Tensor[(10, 20), float32] */) -> Tensor[(10, 20), float32] {\n",
       "  %2 = fn (%p0: Tensor[(10, 20), float32] /* ty=Tensor[(10, 20), float32] */, Primitive=1) -> Tensor[(10, 20), float32] {\n",
       "    %0 = add(%p0, 1f /* ty=float32 */) /* ty=Tensor[(10, 20), float32] */;\n",
       "    %1 = exp(%0) /* ty=Tensor[(10, 20), float32] */;\n",
       "    squeeze(%1) /* ty=Tensor[(10, 20), float32] */\n",
       "  } /* ty=fn (Tensor[(10, 20), float32]) -> Tensor[(10, 20), float32] */;\n",
       "  %2(%x) /* ty=Tensor[(10, 20), float32] */\n",
       "} /* ty=fn (Tensor[(10, 20), float32]) -> Tensor[(10, 20), float32] */"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "def test_conv2d_fuse():\n",
    "    assert tvm.ir.structural_equal(zz, after)\n",
    "\n",
    "\n",
    "def test_concatenate():\n",
    "    assert tvm.ir.structural_equal(zz, after)\n",
    "\n",
    "\n",
    "def test_tuple_root():\n",
    "    assert tvm.ir.structural_equal(zz, after)\n",
    "\n",
    "\n",
    "def test_stop_fusion():\n",
    "    assert tvm.ir.structural_equal(zz, after)\n",
    "\n",
    "\n",
    "def test_fuse_myia_regression():\n",
    "    assert tvm.ir.structural_equal(zz, after)\n",
    "\n",
    "\n",
    "def test_fuse_tuple_get_elemwise():\n",
    "    assert tvm.ir.structural_equal(zz, after)\n",
    "\n",
    "\n",
    "def test_tuple_get_root():\n",
    "    assert tvm.ir.structural_equal(zz, after)\n",
    "\n",
    "\n",
    "def fuse0(mod):\n",
    "    mod = relay.transform.InferType()(mod)\n",
    "    return relay.transform.FuseOps(fuse_opt_level=0)(mod)\n",
    "\n",
    "\n",
    "def fuse2(mod):\n",
    "    mod = relay.transform.InferType()(mod)\n",
    "    return relay.transform.FuseOps(fuse_opt_level=2)(mod)\n",
    "\n",
    "\n",
    "def test_tuple_intermediate():\n",
    "    assert tvm.ir.structural_equal(m[\"main\"], after)\n",
    "\n",
    "\n",
    "def test_tuple_consecutive():\n",
    "    assert tvm.ir.structural_equal(m[\"main\"], after)\n",
    "\n",
    "\n",
    "def test_inception_like():\n",
    "    assert tvm.ir.structural_equal(m[\"main\"], after)\n",
    "\n",
    "\n",
    "def test_fuse_parallel_injective():\n",
    "    assert tvm.ir.structural_equal(zz, after)\n",
    "\n",
    "\n",
    "def test_immutable():\n",
    "    assert tvm.ir.structural_equal(new_mod, transform.InferType()(expected()))\n",
    "\n",
    "\n",
    "def test_split():\n",
    "    mod = transform.FuseOps()(mod)\n",
    "\n",
    "\n",
    "def test_fuse_max():\n",
    "    assert tvm.ir.structural_equal(zz, after)\n",
    "\n",
    "\n",
    "link_params = tvm.testing.parameter(False, True)\n",
    "\n",
    "\n",
    "def test_fuse_take(link_params):\n",
    "    relay.build(m, \"llvm\")\n",
    "\n",
    "\n",
    "def test_fuse_gather_nd(link_params):\n",
    "    relay.build(m, \"llvm\")\n",
    "\n",
    "\n",
    "@tvm.testing.uses_gpu\n",
    "def test_fuse_bcast_reduce_scalar():\n",
    "    assert tvm.ir.structural_equal(m[\"main\"], after)\n",
    "\n",
    "\n",
    "def test_fuse_max_diamond():\n",
    "    assert tvm.ir.structural_equal(fused, expected)\n",
    "\n",
    "\n",
    "def test_fuse_dynamic_squeeze_slice_take():\n",
    "    assert np.allclose(result.numpy(), np_result)\n",
    "\n",
    "\n",
    "@tvm.testing.uses_gpu\n",
    "def test_fuse_softmax():\n",
    "        tvm.testing.assert_allclose(result, ref, rtol=1e-4, atol=1e-4)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tvmz",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
